//=======================================================
// 作者：KingSley
// 描述：通过Coroutine协程、Update计时器、Invoke同步线程实现延迟调用函数
//=======================================================
using UnityEngine;
using System.Collections;
using DG.Tweening;

namespace Babybus.Uno
{
    public class DOTweenCoroutine : MonoBehaviour
    {
        float timer = 7.0f;
        bool isplay = false;

        /// <summary>
        /// 开始执行缩放，并通过DOTween自带延迟功能再次执行缩放功能
        /// 调用Invoke同步线程延迟3秒执行第3次缩放
        /// </summary>
        private void Start()
        {
            transform.DOScale(new Vector3(3, 3, 3), 1f).OnComplete(new TweenCallback(DoScale1));
            Invoke("DoScale2", 3f);
        }
        
        private void Update()
        {
            // Update计时器延迟调用移动函数
            if (Time.realtimeSinceStartup - timer > 1.0f && !isplay)
            {
                transform.DOMove(new Vector3(-3, 1, -3), 1f);
                isplay = true;
                // 协程延迟调用第2次移动函数
                StartCoroutine(DoMove1(3));
            }
        }

        private void DoScale1()
        {
            transform.DOScale(new Vector3(1f, 1f, 1f), 1f);
        }

        private void DoScale2()
        {
            transform.DOScale(new Vector3(2, 2, 2), 1f);
        }

        IEnumerator DoMove1(int times)
        {
            yield return new WaitForSeconds(times);
            transform.DOMove(new Vector3(3, 0, 3), 1f);
        }
    }
}
